package cn.com.cifi.mars.service.impl;

import cn.com.cifi.mars.bean.vo.price.BulidWarnInfoVO;
import cn.com.cifi.mars.bean.vo.price.PriceApproveWarnInfoVO;
import cn.com.cifi.mars.entity.PriceApprovalDifferencesBuild;
import cn.com.cifi.mars.mapper.mars.PriceApprovalDifferencesBuildMapper;
import cn.com.cifi.mars.service.ApprovalPreviewService;
import cn.com.cifi.mars.service.PriceApprovalDifferencesBuildService;
import cn.com.cifi.mars.util.UUIDUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 货值审批楼栋均价差异表 服务实现类
 * </p>
 *
 * @author lyb
 * @since 2021-08-10
 */
@Service
public class PriceApprovalDifferencesBuildServiceImpl extends ServiceImpl<PriceApprovalDifferencesBuildMapper, PriceApprovalDifferencesBuild> implements PriceApprovalDifferencesBuildService {

	@Autowired
	private PriceApprovalDifferencesBuildMapper priceApprovalDifferencesBuildMapper;
	@Autowired
    private ApprovalPreviewService approvalPreviewService;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void savePriceApprovalDifferencesBuildData(String id, String projectId, String versionState) {
		PriceApproveWarnInfoVO vo = approvalPreviewService.getWarnInfo("", id);
		if(vo != null) {
			List<BulidWarnInfoVO> bulidWarnInfoList = vo.getBulidWarnInfoList();
			if(CollectionUtils.isNotEmpty(bulidWarnInfoList)) {
				for(BulidWarnInfoVO obj : bulidWarnInfoList) {
					PriceApprovalDifferencesBuild build = new PriceApprovalDifferencesBuild();
					build.setId(UUIDUtils.create());
					build.setProjectId(projectId);
					build.setBillId(id);
					build.setState(versionState);
					build.setObjName(obj.getObjectName());
					build.setDivergeRate(obj.getDivergeRate());
					build.setStrategyAvg(obj.getStrategyAvg());
					build.setPresidentAvg(obj.getPresidentAvg());
					build.setBeforeAvg(obj.getBeforeAvg());
					build.setAfterAvg(obj.getAfterAvg());
					build.setAfterStrategy(obj.getAfterStrategy());
					build.setAfterPresident(obj.getAfterPresident());
					build.setAfterBfore(obj.getAfterBfore());
					build.setCreateTime(LocalDateTime.now());
					priceApprovalDifferencesBuildMapper.insert(build);
				}
			}
		}
		
	}

	@Override
	public List<PriceApprovalDifferencesBuild> selectByBusinessKey(String businessKey) {

		List<PriceApprovalDifferencesBuild> priceApprovalDifferencesBuilds
				= priceApprovalDifferencesBuildMapper.selectList(new QueryWrapper<PriceApprovalDifferencesBuild>().eq("bill_id",businessKey));

		return priceApprovalDifferencesBuilds;
	}

}
